package com.vip.sys.modular.user.controller;

import com.vip.aop.annotations.LocalLock;
import com.vip.common.annotations.CommonOperationLog;
import com.vip.common.enums.BusinessType;
import com.vip.common.enums.ResponseEnum;
import com.vip.common.response.CommonResponse;
import com.vip.sys.modular.user.entity.User;
import com.vip.sys.modular.user.params.UserAddParams;
import com.vip.sys.modular.user.params.UserAuthorizedRolesParams;
import com.vip.sys.modular.user.params.UserPageParams;
import com.vip.sys.modular.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author tianlianyin
 * @Date 2024/11/9
 */
@RestController
@RequestMapping("/sys/user")
@Tag(name = "用户管理控制器")
public class UserController {
    @Resource
    private UserService userService;
    @PostMapping("/add")
    @Operation(summary = "添加用户")
    @CommonOperationLog(title = "添加用户",businessType = BusinessType.INSERT)
    @LocalLock(key = "user:add:{#user.username}")
    @PreAuthorize("hasAuthority('sys:user:add')")
    public CommonResponse addUser(@RequestBody UserAddParams user){
        return userService.add(user)?CommonResponse.success("添加成功"):CommonResponse.error(ResponseEnum.ERROR,"添加失败，用户已存在");
    }
    @GetMapping("/delete")
    @Operation(summary = "删除用户")
    @CommonOperationLog(title = "删除用户",businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public CommonResponse deleteUser(@RequestParam("id") String id){
        if (userService.delete(id))
            return CommonResponse.success("删除成功");
        return CommonResponse.error(ResponseEnum.ERROR,"删除失败");
    }
    @PostMapping("/edit")
    @Operation(summary = "编辑用户")
    @CommonOperationLog(title = "编辑用户",businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('sys:user:edit')")
    public CommonResponse edit(@RequestBody User user){
        if (userService.edit(user))
            return CommonResponse.success("编辑成功");
        return CommonResponse.error(ResponseEnum.ERROR,"编辑失败");
    }
    @PostMapping("/getAllRole")
    @Operation(summary = "根据用户名查询用户角色")
    public CommonResponse getUserRole(@RequestBody UserPageParams userPageParams){
        return CommonResponse.success(userService.getAllRolesByName(userPageParams.getName()));
    }
    @PostMapping("/getAllResource")
    @Operation(summary = "根据用户名查询用户资源")
    public CommonResponse getUserResource(@RequestBody UserPageParams userPageParams){
        return CommonResponse.success(userService.getAllResourceByName(userPageParams.getName()));
    }
    @GetMapping("/getUserInfo")
    @Operation(summary = "根据用户名获取用户信息")
    public CommonResponse getUserInfo(@RequestParam("username") String username){
        return CommonResponse.success(userService.getUserInfo(username));
    }
    @PostMapping("/uploadAvatar")
    @CommonOperationLog(title = "上传头像",businessType = BusinessType.OTHER)
    @Operation(summary = "上传头像")
    public CommonResponse uploadAvatar(@RequestParam("avatar") MultipartFile avatar){
        if (avatar.isEmpty())
            return CommonResponse.error(ResponseEnum.ERROR,"上传失败，文件为空");
        if (userService.uploadAvatar(avatar))
            return CommonResponse.success("上传成功");
        return CommonResponse.error(ResponseEnum.ERROR,"上传失败");
    }
    @PostMapping("/page")
    @Operation(summary = "分页获取用户列表")
    public CommonResponse page(@RequestBody UserPageParams userPageParams){
        return CommonResponse.success(userService.pageList(userPageParams));
    }
    @PostMapping("/authorizedRoles")
    @Operation(summary = "授权角色")
    @CommonOperationLog(title = "授权角色",businessType = BusinessType.GRANT)
    public CommonResponse authorizedRoles(@RequestBody UserAuthorizedRolesParams userAuthorizedRolesParams){
        return userService.authorizedRoles(userAuthorizedRolesParams)?CommonResponse.success("授权成功"):CommonResponse.error(ResponseEnum.ERROR,"授权失败");
    }
}
